import 'package:flutter/material.dart';
import 'package:v50art/AppStyle.dart';
import 'package:v50art/Util.dart';
import 'package:v50art/service/NetworkService.dart';

class FloatingWidget extends StatefulWidget {
  const FloatingWidget({super.key});

  @override
  State<StatefulWidget> createState() => FloatingWidgetState();
}

class FloatingWidgetState extends State<FloatingWidget> {
  bool isShowWindow = false;
  var urlController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: isShowWindow
          ? Container(
              padding: const EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: Colors.grey),
                  borderRadius: AppStyle.borderRadius),
              width: MediaQuery.of(context).size.width * 0.8,
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      const Padding(
                          padding: EdgeInsets.only(left: 20),
                          child: Text("控制台")),
                      InkWell(
                        onTap: () {
                          setState(() {
                            isShowWindow = false;
                          });
                        },
                        child: const Padding(
                          padding:
                              EdgeInsets.only(bottom: 8, right: 8, left: 8),
                          child: Icon(Icons.minimize),
                        ),
                      )
                    ],
                  ),
                  const Divider(),
                  Container(
                    margin: const EdgeInsets.all(5),
                    padding: const EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        color: Colors.white, boxShadow: AppStyle.boxShadow),
                    child: Column(
                      children: [
                        TextField(
                          controller: urlController,
                          decoration:
                              const InputDecoration(hintText: "请输入baseurl"),
                        ),
                        ElevatedButton(
                            onPressed: () async {
                              if (urlController.text.isEmpty) {
                                Util.toast("请输入babseUrl");
                                return;
                              }
                              await NetworkService.updateUrl(
                                  baseUrl: urlController.text);
                              Util.toast("更新完毕");
                            },
                            child: const Text("更新链接")),
                      ],
                    ),
                  )
                ],
              ),
            )
          : InkWell(
              onTap: () {
                urlController.text = NetworkService.currentBaseUrl;
                setState(() {
                  isShowWindow = true;
                });
              },
              child: Container(
                  padding: const EdgeInsets.all(20),
                  decoration: BoxDecoration(
                      border: Border.all(color: Colors.grey),
                      shape: BoxShape.circle),
                  child: const Icon(
                    Icons.mood_rounded,
                  )),
            ),
    );
  }
}
